
<template>
  <div id="app">
    <el-card class="box-card">
      <div class="logo"><img src="http://likede2-admin.itheima.net/img/logo.595745bd.png" alt="" /></div>
      <!-- 账号 -->
      <el-form ref="loginRef" :model="loginFrom" :rules="loginRules">
        <el-form-item prop="mobile">
          <el-input v-model="loginFrom.mobile" placeholder="请输入账号" prefix-icon="iconfont icon-shouji" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input ref="password" v-model="loginFrom.password" :type="passwordType" placeholder="请输入密码" prefix-icon="iconfont icon-jiesuo" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="rules">
          <el-input v-model="loginFrom.rules" placeholder="请输入验证码" porp="rules" prefix-icon="iconfont icon-anquan" />
          <div class="rules" @click="getImageCode"><img :src="url" /></div>
        </el-form-item>
        <!-- 登录 -->
        <el-button type="primary" style="width:100%" :loading="loading" @click="login">登录</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { imageCode } from '@/api'
import { Message } from 'element-ui'
export default {
  data() {
    return {
      // 登陆账号密码
      loginFrom: {
        mobile: 'admin',
        password: 'admin',
        rules: ''
      },
      // 校验
      loginRules: {
        mobile: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 1, max: 18, message: '密码长度在1到18个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '密码长度在1到20个字符', trigger: 'blur' }
        ],
        rules: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { min: 4, max: 4, message: '请输入4个字符', trigger: 'blur' }
        ]
      },
      passwordType: 'password',
      // 验证码地址
      url: '',
      // 验证码随机数
      random: '',
      // 按钮旋转
      loading: false
    }
  },
  created() {
    this.getImageCode()
    console.log(123)
    console.log(this.$store.state.user.token)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async getImageCode() {
      const random = Math.random() * 10
      this.random = random
      const res = await imageCode(random)
      this.url = res.request.responseURL
    },
    login() {
      this.$refs.loginRef.validate(async valid => {
        if (valid) {
          try {
            this.loading = true
            const res = await this.$store.dispatch('user/fetchLogin', {
              loginName: this.loginFrom.mobile,
              password: this.loginFrom.password,
              loginType: 0,
              code: this.loginFrom.rules,
              clientToken: this.random
            })
            console.log(res)
            if (res.msg === '验证码错误') {
              Message.error('验证码错误')
              this.loading = false
              return
            }
            this.loading = false
            this.$router.push('/')
          } catch (err) {
            console.log(err)
            this.loading = false
          }
        } else {
          console.log('校验失败')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
body,
html {
  background-image: url(http://likede2-admin.itheima.net/img/background.be4fae7d.png);
}
#app {
  display: flex;
  justify-content: center;
  align-items: center;
}

.box-card {
  position: relative;
  width: 518px;
  height: 388px;
  padding: 60px 10px 0;
  overflow: visible;
  .logo {
    position: absolute;
    top: -48px;
    left: 50%;
    margin-left: -48px;
    width: 96px;
    height: 96px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .el-input__inner {
    height: 52px;
  }
  .el-button {
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg, #2e50e1, #6878f0);
    opacity: 0.91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
  }
  .rules {
    position: absolute;
    right: 0;
    top: 0;
    width: 130px;
    height: 50px;
  }
}
.show-pwd {
  position: absolute;
  right: 10px;
  top: 7px;
  font-size: 16px;
  cursor: pointer;
  user-select: none;
}
</style>
